local function SpawnAnimals(inst)
    local animals = {"perd","bunnyman","lightcrab","lightflier","carrat_planted","fruitdragon",
                        "mandrake_active","beefalo","lightninggoat","deer","koalefant_summer","koalefant_winter","catcoon","tumbleweed"}
    local ret_name = inst.npc_base_lib:GetRandom(animals)
    local num = math.random(4,8)
    local points = inst.npc_base_lib:GetSurroundPoints({
        target = inst,
        num = num,
        range = 6
    })
    for k, pt in pairs(points) do
        SpawnPrefab(ret_name).Transform:SetPosition(pt.x,0,pt.z)
        -- SpawnPrefab("collapse_small").Transform:SetPosition(pt.x,0, pt.z)
        SpawnPrefab("npc_fx_collapse"):PushEvent("Set",{
            pt = pt,
            sound_off = true,
            type = "small",
        })
    end
end
local function TrowItems(inst)
    local prop_num = math.random(100)
    local item_table = {}
    local item_num = 1
    if prop_num <= 20 then
        local items = inst.npc_base_lib:Get_Special_Materials()
        table.insert(item_table,inst.npc_base_lib:GetRandom(items))
        table.insert(item_table,inst.npc_base_lib:GetRandom(items))
        table.insert(item_table,inst.npc_base_lib:GetRandom(items))
        table.insert(item_table,inst.npc_base_lib:GetRandom(items))
        item_num = 3
        if TUNING.NPC_CONFIG.REWARDS_LESS ~= true then
            item_num = 6
        end
    elseif prop_num <= 40 then
        table.insert(item_table,inst.npc_base_lib:GetRandom(inst.npc_base_lib:Get_Equipments_Hand()))
        table.insert(item_table,inst.npc_base_lib:GetRandom(inst.npc_base_lib:Get_Equipments_Hats()))
        table.insert(item_table,inst.npc_base_lib:GetRandom(inst.npc_base_lib:Get_Equipments_Body()))
        item_num = 1
    else
        item_num = 4
        if TUNING.NPC_CONFIG.REWARDS_LESS ~= true then
            item_num = 6
        end
        for i = 1, item_num, 1 do
            table.insert(item_table,inst.npc_base_lib:GetRandom(inst.npc_base_lib:Get_Cooked_Food_List()))            
        end
    end
    
    for k, name in pairs(item_table) do
        inst.npc_base_lib:Throw_Out_Items({
                target = inst,
                name = name,
                num = item_num ~= 1 and math.random(item_num) or 1,
                range = 4,
                height = 5,
                random_height = true
        })
    end
    
end

local function start_event(inst,player)
    if math.random(100) <= 50 then
        SpawnAnimals(player)
    else
        TrowItems(player)
    end
    inst:Remove()
end
local function fx()
    local inst = CreateEntity()
    inst.entity:AddTransform()

    inst:AddTag("INLIMBO")
    inst:AddTag("FX")

    if not TheWorld.ismastersim then
        return inst
    end

    inst:ListenForEvent("Set",function(inst,player)
        start_event(inst,player)
    end)

    return inst
end

return Prefab("npc_card_supplies",fx)